package com.pxmeta.admin.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.party.entity.Gift;
import com.pxmeta.party.entity.GiftLottery;
import com.pxmeta.party.entity.LotteryLuckyUser;
import com.pxmeta.party.entity.TurntableNumber;
import com.pxmeta.party.entity.dto.AddGiftDTO;
import com.pxmeta.party.entity.dto.GiveLotteryGiftDTO;
import com.pxmeta.party.entity.dto.UpdateGiftDTO;
import com.pxmeta.party.entity.dto.UpdateGiftDTO;
import com.pxmeta.party.entity.vo.GiftLotteryVO;
import com.pxmeta.party.service.IGiftLotteryService;
import com.pxmeta.party.service.IGiftService;
import com.pxmeta.party.service.ILotteryLuckyUserService;
import com.pxmeta.party.service.ITurntableNumberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

@RestController
@Api(value = "礼物管理",tags = "礼物")
@CrossOrigin
@RequestMapping(value = "/gift")
@RequiredArgsConstructor
public class GiftController {

    private final IGiftService giftService;

    private final IGiftLotteryService giftLotteryService;
    private final ITurntableNumberService turntableNumberService;

    private final ILotteryLuckyUserService lotteryLuckyUserService;

    @GetMapping("/getGiftList")
    @ApiOperation("获取礼物列表")
    public AjaxResult<List<Gift>> getGiftList(@RequestParam Integer type,
                                  @RequestParam Integer page,
                                  @RequestParam Integer size){
        IPage<Gift> result = giftService.getGiftProbability(page, size, type);

        return AjaxResult.successPage(result.getRecords(), result.getTotal());
    }


    @GetMapping("/getItemList")
    @ApiOperation("获取盲盒礼物列表")
    public AjaxResult<List<Gift>> getItemList(@RequestParam Integer giftId){
        List<Gift> result = giftService.getItemList(giftId);
        return AjaxResult.success(result);
    }


    @PostMapping("/addGift")
    @ApiOperation("添加礼物")
    public AjaxResult addGift(@RequestBody AddGiftDTO dto){
        giftService.addGift(dto);
        return AjaxResult.success();
    }

//    @PutMapping("/updateGift")
//    @ApiOperation("修改礼物")
//    public AjaxResult updateGift(@RequestBody UpdateGiftDTO dto){
//        giftService.updateGift(dto);
//        return AjaxResult.success();
//    }


    @PostMapping("/giveLotteryGift")
    @ApiOperation("设置用户下次必中礼物")
    public AjaxResult giveLotteryGift(@RequestBody GiveLotteryGiftDTO dto){
        giftService.giveLotteryGift(dto);
        return AjaxResult.success();
    }

//    @PutMapping("/updateGift")
//    @ApiOperation("修改礼物")
//    public AjaxResult updateGift(@RequestBody UpdateGiftDTO dto){
//        giftService.updateGift(dto);
//        return AjaxResult.success();
//    }


    @DeleteMapping("/deleteGiftById/{id}")
    @ApiOperation("删除礼物")
    public AjaxResult deleteGiftById(@PathVariable Long id){
        giftService.removeById(id);
        return AjaxResult.success();
    }

    @PutMapping("/hiddenGift/{id}")
    @ApiOperation("关闭礼物")
    public AjaxResult hiddenGift(@PathVariable Long id){
        Gift gift = giftService.getById(id);
        gift.setStatus(0);
        giftService.updateById(gift);
        return AjaxResult.success();
    }

    @PutMapping("/showGift/{id}")
    @ApiOperation("开启礼物")
    public AjaxResult showGift(@PathVariable Long id){
        Gift gift = giftService.getById(id);
        gift.setStatus(1);
        giftService.updateById(gift);
        return AjaxResult.success();
    }


    @PutMapping("/updateGift")
    @ApiOperation("修改礼物")
    public AjaxResult updateGift(@RequestBody Gift gift){
        if (gift.getProbability()!=null || gift.getCount()!=null || gift.getConditionCount()!=null || gift.getRemainingQuantity() != null){
            //修改奖品概率
            giftLotteryService.updateGiftLottery(gift.getId(),gift.getProbability(),gift.getCount(),gift.getConditionCount(), gift.getRemainingQuantity());
        }

        giftService.updateById(gift);
        return AjaxResult.success();
    }

    @GetMapping("/getLotteryList")
    @ApiOperation("获取中奖记录")
    public AjaxResult getLotteryList(@RequestParam Integer page,
                                     @RequestParam Integer size,
                                     @RequestParam(required = false) Integer type,
                                     @RequestParam(required = false) String keyword){

        IPage<LotteryLuckyUser> result = lotteryLuckyUserService.getLotteryList(page,size,type,keyword);
        return AjaxResult.successPage(result.getRecords(),result.getTotal());
    }


    @GetMapping("/getLotteryItemList")
    @ApiOperation("获取奖品记录")
    public AjaxResult getLotteryItemList(@RequestParam Long recordId){
        List<Gift> list = lotteryLuckyUserService.getLotteryItemList(recordId);
        return AjaxResult.success(list);
    }

    @GetMapping("/getCurrentLottery")
    @ApiOperation("获取本期抽奖详情")
    public AjaxResult getCurrentLottery(@RequestParam(required = false,defaultValue = "1") Integer type){
        TurntableNumber turntableNumber0 = turntableNumberService.getTurntableNumber(0);
        TurntableNumber turntableNumber = turntableNumberService.getTurntableNumber(1);
        TurntableNumber turntableNumber2 = turntableNumberService.getTurntableNumber(2);
        TurntableNumber turntableNumber3 = turntableNumberService.getTurntableNumber(3);
        TurntableNumber turntableNumber4 = turntableNumberService.getTurntableNumber(4);
        TurntableNumber turntableNumber5 = turntableNumberService.getTurntableNumber(5);
        List<TurntableNumber> list = Lists.newLinkedList();
        list.add(turntableNumber0);
        list.add(turntableNumber);
        list.add(turntableNumber2);
        list.add(turntableNumber3);
        list.add(turntableNumber4);
        list.add(turntableNumber5);
        return AjaxResult.success(list);
    }



    @GetMapping("/getLotteryProbability")
    @ApiOperation("获取抽奖概率")
    public AjaxResult getLotteryProbability(@RequestParam Integer type){
        List<GiftLotteryVO> list = Lists.newLinkedList();
        if (type == 1){ //宝箱
            list = giftService.getRewardList(9999, Boolean.TRUE);
        }else if (type == 2){//欢乐转盘
            list = giftService.getRewardList(2000,Boolean.TRUE);
        }else if (type == 3){ //黄金转盘
            list = giftService.getRewardList(1000,Boolean.TRUE);
        }
        return AjaxResult.success(list);
    }


    @PutMapping("/updateLottery")
    @ApiOperation("修改概率")
    public AjaxResult updateProbability(@RequestBody GiftLottery giftLottery){
        giftLotteryService.updateById(giftLottery);
        return AjaxResult.success();
    }


    @GetMapping("/updateAdAstraMoney")
    @ApiOperation("添加垫盘金")
    public AjaxResult updateAdAstraMoney(@RequestParam Integer status,
                                         @RequestParam Integer money){
        turntableNumberService.updateAdAstraMoney(status, money);
        return AjaxResult.success();
    }

    @GetMapping("/updateAdAstraMoneyProbability")
    @ApiOperation("添加垫盘金出奖率")
    public AjaxResult updateAdAstraMoneyProbability(@RequestParam Double probability){
        turntableNumberService.updateAdAstraMoneyProbability(probability);
        return AjaxResult.success();
    }









}
